# 统计一个文本中单词频次最高的10个单词？
import re


class Solution():
    def MaxWord(self, file_name):
        """
        :param file_name: 文件名
        :return:
        """
        with open(file_name, 'r',encoding='UTF-8') as file:
            for lines in file:
                lines = re.sub('\W+', ' ', lines)
            print(lines)
            dict_word = {}
            for i in range(0, len(lines.split(' '))):
                word = lines.split(' ')[i]
                if word not in dict_word:
                    dict_word[word] = 1
                else:
                    dict_word[word] += 1
            word_sorted = sorted(dict_word.items(), key=lambda x: x[1], reverse=True)
            for i in range(0, 10):
                print(word_sorted[i])


if __name__ == '__main__':
    solution = Solution()
    file_name = 'C:\\Users\\zealo\Desktop\\人工智能比赛\\case2_data\\试题数据\\第二部分\\data.txt'
    solution.MaxWord(file_name)